package com.sunyur.tailor.channel.web.outer.returnorder.controller;


import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.returnorder.param.CreateReturnOrderParam;
import com.sunyur.tailor.channel.service.inner.returnorder.service.SunyurReturnOrderService;
import com.sunyur.tailor.channel.web.outer.returnorder.dto.CreateReturnOrderDTO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;


/**
 * @Author pengbing
 * @Description //创建退货单
 * @Date 17:05 2021/12/30
 * @Param
 * @return
 **/
@Log4j2
@RestController
@RequestMapping("/outer/returnOrder")
public class ReturnOrderController {
    /**
     *退货单服务
     */
    @Autowired
    private SunyurReturnOrderService sunyurReturnOrderService;

    /**
     *创建退货单
     */
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult create(@RequestBody @Valid CreateReturnOrderDTO createReturnOrderDTO) {
        log.info("ReturnOrderController create param:{}", JSON.toJSONString(createReturnOrderDTO));
        ChannelResult result = new ChannelResult<>();
        if (Objects.isNull(createReturnOrderDTO)) {
            log.error("ReturnOrderController create param is null");
            result.setError(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
            return result;
        }
        try {
            CreateReturnOrderParam createReturnOrderParam = new CreateReturnOrderParam();
            BeanUtils.copyProperties(createReturnOrderDTO,createReturnOrderParam);
            result = sunyurReturnOrderService.createReturnOrder(createReturnOrderParam);
        } catch (Exception e) {
            log.error("ReturnOrderController create exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }


}
